{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "            row_id       x       y  accuracy    time    place_id\n0                0  0.7941  9.0809        54  470702  8523065625\n1                1  5.9567  4.7968        13  186555  1757726713\n2                2  8.3078  7.0407        74  322648  1137537235\n3                3  7.3665  2.5165        65  704587  6567393236\n4                4  4.0961  1.1307        31  472130  7440663949\n...            ...     ...     ...       ...     ...         ...\n29118016  29118016  6.5133  1.1435        67  399740  8671361106\n29118017  29118017  5.9186  4.4134        67  125480  9077887898\n29118018  29118018  2.9993  6.3680        67  737758  2838334300\n29118019  29118019  4.0637  8.0061        70  764975  1007355847\n29118020  29118020  7.4523  2.0871        17  102842  7028698129\n\n[29118021 rows x 6 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>0.7941</td>\n      <td>9.0809</td>\n      <td>54</td>\n      <td>470702</td>\n      <td>8523065625</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>5.9567</td>\n      <td>4.7968</td>\n      <td>13</td>\n      <td>186555</td>\n      <td>1757726713</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>8.3078</td>\n      <td>7.0407</td>\n      <td>74</td>\n      <td>322648</td>\n      <td>1137537235</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>7.3665</td>\n      <td>2.5165</td>\n      <td>65</td>\n      <td>704587</td>\n      <td>6567393236</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>4.0961</td>\n      <td>1.1307</td>\n      <td>31</td>\n      <td>472130</td>\n      <td>7440663949</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>29118016</th>\n      <td>29118016</td>\n      <td>6.5133</td>\n      <td>1.1435</td>\n      <td>67</td>\n      <td>399740</td>\n      <td>8671361106</td>\n    </tr>\n    <tr>\n      <th>29118017</th>\n      <td>29118017</td>\n      <td>5.9186</td>\n      <td>4.4134</td>\n      <td>67</td>\n      <td>125480</td>\n      <td>9077887898</td>\n    </tr>\n    <tr>\n      <th>29118018</th>\n      <td>29118018</td>\n      <td>2.9993</td>\n      <td>6.3680</td>\n      <td>67</td>\n      <td>737758</td>\n      <td>2838334300</td>\n    </tr>\n    <tr>\n      <th>29118019</th>\n      <td>29118019</td>\n      <td>4.0637</td>\n      <td>8.0061</td>\n      <td>70</td>\n      <td>764975</td>\n      <td>1007355847</td>\n    </tr>\n    <tr>\n      <th>29118020</th>\n      <td>29118020</td>\n      <td>7.4523</td>\n      <td>2.0871</td>\n      <td>17</td>\n      <td>102842</td>\n      <td>7028698129</td>\n    </tr>\n  </tbody>\n</table>\n<p>29118021 rows × 6 columns</p>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\data\\\\train.csv\")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "data": {
      "text/plain": "   row_id       x       y  accuracy    time    place_id\n0       0  0.7941  9.0809        54  470702  8523065625\n1       1  5.9567  4.7968        13  186555  1757726713\n2       2  8.3078  7.0407        74  322648  1137537235\n3       3  7.3665  2.5165        65  704587  6567393236\n4       4  4.0961  1.1307        31  472130  7440663949",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0</td>\n      <td>0.7941</td>\n      <td>9.0809</td>\n      <td>54</td>\n      <td>470702</td>\n      <td>8523065625</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>5.9567</td>\n      <td>4.7968</td>\n      <td>13</td>\n      <td>186555</td>\n      <td>1757726713</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>2</td>\n      <td>8.3078</td>\n      <td>7.0407</td>\n      <td>74</td>\n      <td>322648</td>\n      <td>1137537235</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>3</td>\n      <td>7.3665</td>\n      <td>2.5165</td>\n      <td>65</td>\n      <td>704587</td>\n      <td>6567393236</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4</td>\n      <td>4.0961</td>\n      <td>1.1307</td>\n      <td>31</td>\n      <td>472130</td>\n      <td>7440663949</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "data": {
      "text/plain": "            row_id       x       y  accuracy    time    place_id\n112            112  2.2360  1.3655        66  623174  7663031065\n180            180  2.2003  1.2541        65  610195  2358558474\n367            367  2.4108  1.3213        74  579667  6644108708\n874            874  2.0822  1.1973       320  143566  3229876087\n1022          1022  2.0160  1.1659        65  207993  3244363975\n...            ...     ...     ...       ...     ...         ...\n29115112  29115112  2.1889  1.2914       168  721885  4606837364\n29115204  29115204  2.1193  1.4692        58  563389  2074133146\n29115338  29115338  2.0007  1.4852        25  765986  6691588909\n29115464  29115464  2.4132  1.4237        61  151918  7396159924\n29117493  29117493  2.2948  1.0504        81   79569  1168869217\n\n[83197 rows x 6 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>112</th>\n      <td>112</td>\n      <td>2.2360</td>\n      <td>1.3655</td>\n      <td>66</td>\n      <td>623174</td>\n      <td>7663031065</td>\n    </tr>\n    <tr>\n      <th>180</th>\n      <td>180</td>\n      <td>2.2003</td>\n      <td>1.2541</td>\n      <td>65</td>\n      <td>610195</td>\n      <td>2358558474</td>\n    </tr>\n    <tr>\n      <th>367</th>\n      <td>367</td>\n      <td>2.4108</td>\n      <td>1.3213</td>\n      <td>74</td>\n      <td>579667</td>\n      <td>6644108708</td>\n    </tr>\n    <tr>\n      <th>874</th>\n      <td>874</td>\n      <td>2.0822</td>\n      <td>1.1973</td>\n      <td>320</td>\n      <td>143566</td>\n      <td>3229876087</td>\n    </tr>\n    <tr>\n      <th>1022</th>\n      <td>1022</td>\n      <td>2.0160</td>\n      <td>1.1659</td>\n      <td>65</td>\n      <td>207993</td>\n      <td>3244363975</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>29115112</th>\n      <td>29115112</td>\n      <td>2.1889</td>\n      <td>1.2914</td>\n      <td>168</td>\n      <td>721885</td>\n      <td>4606837364</td>\n    </tr>\n    <tr>\n      <th>29115204</th>\n      <td>29115204</td>\n      <td>2.1193</td>\n      <td>1.4692</td>\n      <td>58</td>\n      <td>563389</td>\n      <td>2074133146</td>\n    </tr>\n    <tr>\n      <th>29115338</th>\n      <td>29115338</td>\n      <td>2.0007</td>\n      <td>1.4852</td>\n      <td>25</td>\n      <td>765986</td>\n      <td>6691588909</td>\n    </tr>\n    <tr>\n      <th>29115464</th>\n      <td>29115464</td>\n      <td>2.4132</td>\n      <td>1.4237</td>\n      <td>61</td>\n      <td>151918</td>\n      <td>7396159924</td>\n    </tr>\n    <tr>\n      <th>29117493</th>\n      <td>29117493</td>\n      <td>2.2948</td>\n      <td>1.0504</td>\n      <td>81</td>\n      <td>79569</td>\n      <td>1168869217</td>\n    </tr>\n  </tbody>\n</table>\n<p>83197 rows × 6 columns</p>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1.数据清洗\n",
    "df = df.query(\"x< 2.5 and x > 2 and y > 1.0 and y < 1.5\")\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "data": {
      "text/plain": "112        1970-01-08 05:06:14\n180        1970-01-08 01:29:55\n367        1970-01-07 17:01:07\n874        1970-01-02 15:52:46\n1022       1970-01-03 09:46:33\n                   ...        \n29115112   1970-01-09 08:31:25\n29115204   1970-01-07 12:29:49\n29115338   1970-01-09 20:46:26\n29115464   1970-01-02 18:11:58\n29117493   1970-01-01 22:06:09\nName: time, Length: 83197, dtype: datetime64[ns]"
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.特征工程--特征拆分\n",
    "df.isnull().sum()\n",
    "df.isnull().any()\n",
    "time_value = pd.to_datetime(df[\"time\"],unit=\"s\")\n",
    "time_value"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1392\\3393731950.py:2: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df[\"day\"] = date.day\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1392\\3393731950.py:3: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df[\"hour\"] = date.hour\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1392\\3393731950.py:4: FutureWarning: weekofyear and week have been deprecated, please use DatetimeIndex.isocalendar().week instead, which returns a Series. To exactly reproduce the behavior of week and weekofyear and return an Index, you may call pd.Int64Index(idx.isocalendar().week)\n",
      "  df[\"week\"] = date.week\n",
      "C:\\Users\\Administrator\\AppData\\Local\\Temp\\ipykernel_1392\\3393731950.py:4: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  df[\"week\"] = date.week\n"
     ]
    },
    {
     "data": {
      "text/plain": "            row_id       x       y  accuracy    time    place_id  day  hour  \\\n112            112  2.2360  1.3655        66  623174  7663031065    8     5   \n180            180  2.2003  1.2541        65  610195  2358558474    8     1   \n367            367  2.4108  1.3213        74  579667  6644108708    7    17   \n874            874  2.0822  1.1973       320  143566  3229876087    2    15   \n1022          1022  2.0160  1.1659        65  207993  3244363975    3     9   \n...            ...     ...     ...       ...     ...         ...  ...   ...   \n29115112  29115112  2.1889  1.2914       168  721885  4606837364    9     8   \n29115204  29115204  2.1193  1.4692        58  563389  2074133146    7    12   \n29115338  29115338  2.0007  1.4852        25  765986  6691588909    9    20   \n29115464  29115464  2.4132  1.4237        61  151918  7396159924    2    18   \n29117493  29117493  2.2948  1.0504        81   79569  1168869217    1    22   \n\n          week  \n112          2  \n180          2  \n367          2  \n874          1  \n1022         1  \n...        ...  \n29115112     2  \n29115204     2  \n29115338     2  \n29115464     1  \n29117493     1  \n\n[83197 rows x 9 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>week</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>112</th>\n      <td>112</td>\n      <td>2.2360</td>\n      <td>1.3655</td>\n      <td>66</td>\n      <td>623174</td>\n      <td>7663031065</td>\n      <td>8</td>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>180</th>\n      <td>180</td>\n      <td>2.2003</td>\n      <td>1.2541</td>\n      <td>65</td>\n      <td>610195</td>\n      <td>2358558474</td>\n      <td>8</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>367</th>\n      <td>367</td>\n      <td>2.4108</td>\n      <td>1.3213</td>\n      <td>74</td>\n      <td>579667</td>\n      <td>6644108708</td>\n      <td>7</td>\n      <td>17</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>874</th>\n      <td>874</td>\n      <td>2.0822</td>\n      <td>1.1973</td>\n      <td>320</td>\n      <td>143566</td>\n      <td>3229876087</td>\n      <td>2</td>\n      <td>15</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1022</th>\n      <td>1022</td>\n      <td>2.0160</td>\n      <td>1.1659</td>\n      <td>65</td>\n      <td>207993</td>\n      <td>3244363975</td>\n      <td>3</td>\n      <td>9</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>29115112</th>\n      <td>29115112</td>\n      <td>2.1889</td>\n      <td>1.2914</td>\n      <td>168</td>\n      <td>721885</td>\n      <td>4606837364</td>\n      <td>9</td>\n      <td>8</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>29115204</th>\n      <td>29115204</td>\n      <td>2.1193</td>\n      <td>1.4692</td>\n      <td>58</td>\n      <td>563389</td>\n      <td>2074133146</td>\n      <td>7</td>\n      <td>12</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>29115338</th>\n      <td>29115338</td>\n      <td>2.0007</td>\n      <td>1.4852</td>\n      <td>25</td>\n      <td>765986</td>\n      <td>6691588909</td>\n      <td>9</td>\n      <td>20</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>29115464</th>\n      <td>29115464</td>\n      <td>2.4132</td>\n      <td>1.4237</td>\n      <td>61</td>\n      <td>151918</td>\n      <td>7396159924</td>\n      <td>2</td>\n      <td>18</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>29117493</th>\n      <td>29117493</td>\n      <td>2.2948</td>\n      <td>1.0504</td>\n      <td>81</td>\n      <td>79569</td>\n      <td>1168869217</td>\n      <td>1</td>\n      <td>22</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n<p>83197 rows × 9 columns</p>\n</div>"
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "date = pd.DatetimeIndex(time_value)\n",
    "df[\"day\"] = date.day\n",
    "df[\"hour\"] = date.hour\n",
    "df[\"week\"] = date.week\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "            row_id       x       y  accuracy    time    place_id  day  hour  \\\n112            112  2.2360  1.3655        66  623174  7663031065    8     5   \n367            367  2.4108  1.3213        74  579667  6644108708    7    17   \n874            874  2.0822  1.1973       320  143566  3229876087    2    15   \n1022          1022  2.0160  1.1659        65  207993  3244363975    3     9   \n1045          1045  2.3859  1.1660       498  503378  6438240873    6    19   \n...            ...     ...     ...       ...     ...         ...  ...   ...   \n29115112  29115112  2.1889  1.2914       168  721885  4606837364    9     8   \n29115204  29115204  2.1193  1.4692        58  563389  2074133146    7    12   \n29115338  29115338  2.0007  1.4852        25  765986  6691588909    9    20   \n29115464  29115464  2.4132  1.4237        61  151918  7396159924    2    18   \n29117493  29117493  2.2948  1.0504        81   79569  1168869217    1    22   \n\n          week  \n112          2  \n367          2  \n874          1  \n1022         1  \n1045         2  \n...        ...  \n29115112     2  \n29115204     2  \n29115338     2  \n29115464     1  \n29117493     1  \n\n[80910 rows x 9 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>row_id</th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>time</th>\n      <th>place_id</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>week</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>112</th>\n      <td>112</td>\n      <td>2.2360</td>\n      <td>1.3655</td>\n      <td>66</td>\n      <td>623174</td>\n      <td>7663031065</td>\n      <td>8</td>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>367</th>\n      <td>367</td>\n      <td>2.4108</td>\n      <td>1.3213</td>\n      <td>74</td>\n      <td>579667</td>\n      <td>6644108708</td>\n      <td>7</td>\n      <td>17</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>874</th>\n      <td>874</td>\n      <td>2.0822</td>\n      <td>1.1973</td>\n      <td>320</td>\n      <td>143566</td>\n      <td>3229876087</td>\n      <td>2</td>\n      <td>15</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1022</th>\n      <td>1022</td>\n      <td>2.0160</td>\n      <td>1.1659</td>\n      <td>65</td>\n      <td>207993</td>\n      <td>3244363975</td>\n      <td>3</td>\n      <td>9</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1045</th>\n      <td>1045</td>\n      <td>2.3859</td>\n      <td>1.1660</td>\n      <td>498</td>\n      <td>503378</td>\n      <td>6438240873</td>\n      <td>6</td>\n      <td>19</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>29115112</th>\n      <td>29115112</td>\n      <td>2.1889</td>\n      <td>1.2914</td>\n      <td>168</td>\n      <td>721885</td>\n      <td>4606837364</td>\n      <td>9</td>\n      <td>8</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>29115204</th>\n      <td>29115204</td>\n      <td>2.1193</td>\n      <td>1.4692</td>\n      <td>58</td>\n      <td>563389</td>\n      <td>2074133146</td>\n      <td>7</td>\n      <td>12</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>29115338</th>\n      <td>29115338</td>\n      <td>2.0007</td>\n      <td>1.4852</td>\n      <td>25</td>\n      <td>765986</td>\n      <td>6691588909</td>\n      <td>9</td>\n      <td>20</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>29115464</th>\n      <td>29115464</td>\n      <td>2.4132</td>\n      <td>1.4237</td>\n      <td>61</td>\n      <td>151918</td>\n      <td>7396159924</td>\n      <td>2</td>\n      <td>18</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>29117493</th>\n      <td>29117493</td>\n      <td>2.2948</td>\n      <td>1.0504</td>\n      <td>81</td>\n      <td>79569</td>\n      <td>1168869217</td>\n      <td>1</td>\n      <td>22</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n<p>80910 rows × 9 columns</p>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3）过滤签到次数少的地点\n",
    "place_count = df.groupby(\"place_id\").count()[\"row_id\"]\n",
    "place_count\n",
    "\n",
    "# 获取到签到大于3的数据\n",
    "# place_count[place_count>3]\n",
    "df_final = df[df[\"place_id\"].isin(place_count[place_count>3].index.values)]\n",
    "df_final"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "112         7663031065\n180         2358558474\n367         6644108708\n874         3229876087\n1022        3244363975\n               ...    \n29115112    4606837364\n29115204    2074133146\n29115338    6691588909\n29115464    7396159924\n29117493    1168869217\nName: place_id, Length: 83197, dtype: int64"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 特征选择\n",
    "X = df[[\"x\",\"y\",\"accuracy\",\"day\",\"hour\",\"week\"]]\n",
    "y = df[\"place_id\"]\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "               x       y  accuracy  day  hour  week\n23134689  2.4984  1.2480        64    9     1     2\n8200606   2.3825  1.0619       159    5     5     2\n4327901   2.3273  1.2561         1    1    17     1\n18215496  2.3413  1.2970         5    4     5     1\n6791347   2.2177  1.4161        23    2    17     1\n...          ...     ...       ...  ...   ...   ...\n21208817  2.0224  1.3038        10    3     3     1\n24153256  2.0096  1.3358        62    6    19     2\n15484754  2.4788  1.4655        64    8     6     2\n14793946  2.4868  1.4909        70    1    12     1\n19503726  2.4188  1.3279       159    1     2     1\n\n[62397 rows x 6 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>x</th>\n      <th>y</th>\n      <th>accuracy</th>\n      <th>day</th>\n      <th>hour</th>\n      <th>week</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>23134689</th>\n      <td>2.4984</td>\n      <td>1.2480</td>\n      <td>64</td>\n      <td>9</td>\n      <td>1</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>8200606</th>\n      <td>2.3825</td>\n      <td>1.0619</td>\n      <td>159</td>\n      <td>5</td>\n      <td>5</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4327901</th>\n      <td>2.3273</td>\n      <td>1.2561</td>\n      <td>1</td>\n      <td>1</td>\n      <td>17</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>18215496</th>\n      <td>2.3413</td>\n      <td>1.2970</td>\n      <td>5</td>\n      <td>4</td>\n      <td>5</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>6791347</th>\n      <td>2.2177</td>\n      <td>1.4161</td>\n      <td>23</td>\n      <td>2</td>\n      <td>17</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>21208817</th>\n      <td>2.0224</td>\n      <td>1.3038</td>\n      <td>10</td>\n      <td>3</td>\n      <td>3</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>24153256</th>\n      <td>2.0096</td>\n      <td>1.3358</td>\n      <td>62</td>\n      <td>6</td>\n      <td>19</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>15484754</th>\n      <td>2.4788</td>\n      <td>1.4655</td>\n      <td>64</td>\n      <td>8</td>\n      <td>6</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>14793946</th>\n      <td>2.4868</td>\n      <td>1.4909</td>\n      <td>70</td>\n      <td>1</td>\n      <td>12</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>19503726</th>\n      <td>2.4188</td>\n      <td>1.3279</td>\n      <td>159</td>\n      <td>1</td>\n      <td>2</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n<p>62397 rows × 6 columns</p>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 数据拆分\n",
    "from sklearn.model_selection import train_test_split\n",
    "x_train, x_test, y_train, y_test = train_test_split(X, y)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 1.79668197,  0.0025364 , -0.16819357,  1.40218126, -1.52287825,\n         0.82767092],\n       [ 0.99283566, -1.35577199,  0.66133758, -0.10009003, -0.94226048,\n         0.82767092],\n       [ 0.60998565,  0.06165675, -0.7183037 , -1.60236131,  0.79959285,\n        -1.20820965],\n       ...,\n       [ 1.66074247,  1.59002739, -0.16819357,  1.02661344, -0.79710603,\n         0.82767092],\n       [ 1.71622798,  1.77541715, -0.11580213, -1.60236131,  0.07382063,\n        -1.20820965],\n       [ 1.24460116,  0.58571125,  0.66133758, -1.60236131, -1.37772381,\n        -1.20820965]])"
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": "array([[ 0.9539958 ,  0.50907376, -0.64844844, -0.47565785,  0.21897508,\n        -1.20820965],\n       [-0.90268804, -0.48283441, -0.19438929, -1.60236131, -1.52287825,\n        -1.20820965],\n       [ 0.68489109, -0.13322145, -0.63971654,  0.2754778 ,  1.67051952,\n         0.82767092],\n       ...,\n       [ 0.01351643,  0.84700862, -0.69210798, -0.10009003, -0.50679714,\n         0.82767092],\n       [-0.45741683,  1.77906655,  0.46050372,  1.02661344, -1.08741492,\n         0.82767092],\n       [ 1.1121295 , -0.51932846, -0.2118531 ,  0.65104562,  0.79959285,\n         0.82767092]])"
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 特征工程标准化\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "s = StandardScaler()\n",
    "x_train = s.fit_transform(x_train)\n",
    "x_test = s.transform(x_test)\n",
    "display(x_train,x_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "data": {
      "text/plain": "KNeighborsClassifier()",
      "text/html": "<style>#sk-container-id-1 {color: black;}#sk-container-id-1 pre{padding: 0;}#sk-container-id-1 div.sk-toggleable {background-color: white;}#sk-container-id-1 label.sk-toggleable__label {cursor: pointer;display: block;width: 100%;margin-bottom: 0;padding: 0.3em;box-sizing: border-box;text-align: center;}#sk-container-id-1 label.sk-toggleable__label-arrow:before {content: \"▸\";float: left;margin-right: 0.25em;color: #696969;}#sk-container-id-1 label.sk-toggleable__label-arrow:hover:before {color: black;}#sk-container-id-1 div.sk-estimator:hover label.sk-toggleable__label-arrow:before {color: black;}#sk-container-id-1 div.sk-toggleable__content {max-height: 0;max-width: 0;overflow: hidden;text-align: left;background-color: #f0f8ff;}#sk-container-id-1 div.sk-toggleable__content pre {margin: 0.2em;color: black;border-radius: 0.25em;background-color: #f0f8ff;}#sk-container-id-1 input.sk-toggleable__control:checked~div.sk-toggleable__content {max-height: 200px;max-width: 100%;overflow: auto;}#sk-container-id-1 input.sk-toggleable__control:checked~label.sk-toggleable__label-arrow:before {content: \"▾\";}#sk-container-id-1 div.sk-estimator input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-label input.sk-toggleable__control:checked~label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 input.sk-hidden--visually {border: 0;clip: rect(1px 1px 1px 1px);clip: rect(1px, 1px, 1px, 1px);height: 1px;margin: -1px;overflow: hidden;padding: 0;position: absolute;width: 1px;}#sk-container-id-1 div.sk-estimator {font-family: monospace;background-color: #f0f8ff;border: 1px dotted black;border-radius: 0.25em;box-sizing: border-box;margin-bottom: 0.5em;}#sk-container-id-1 div.sk-estimator:hover {background-color: #d4ebff;}#sk-container-id-1 div.sk-parallel-item::after {content: \"\";width: 100%;border-bottom: 1px solid gray;flex-grow: 1;}#sk-container-id-1 div.sk-label:hover label.sk-toggleable__label {background-color: #d4ebff;}#sk-container-id-1 div.sk-serial::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: 0;}#sk-container-id-1 div.sk-serial {display: flex;flex-direction: column;align-items: center;background-color: white;padding-right: 0.2em;padding-left: 0.2em;position: relative;}#sk-container-id-1 div.sk-item {position: relative;z-index: 1;}#sk-container-id-1 div.sk-parallel {display: flex;align-items: stretch;justify-content: center;background-color: white;position: relative;}#sk-container-id-1 div.sk-item::before, #sk-container-id-1 div.sk-parallel-item::before {content: \"\";position: absolute;border-left: 1px solid gray;box-sizing: border-box;top: 0;bottom: 0;left: 50%;z-index: -1;}#sk-container-id-1 div.sk-parallel-item {display: flex;flex-direction: column;z-index: 1;position: relative;background-color: white;}#sk-container-id-1 div.sk-parallel-item:first-child::after {align-self: flex-end;width: 50%;}#sk-container-id-1 div.sk-parallel-item:last-child::after {align-self: flex-start;width: 50%;}#sk-container-id-1 div.sk-parallel-item:only-child::after {width: 0;}#sk-container-id-1 div.sk-dashed-wrapped {border: 1px dashed gray;margin: 0 0.4em 0.5em 0.4em;box-sizing: border-box;padding-bottom: 0.4em;background-color: white;}#sk-container-id-1 div.sk-label label {font-family: monospace;font-weight: bold;display: inline-block;line-height: 1.2em;}#sk-container-id-1 div.sk-label-container {text-align: center;}#sk-container-id-1 div.sk-container {/* jupyter's `normalize.less` sets `[hidden] { display: none; }` but bootstrap.min.css set `[hidden] { display: none !important; }` so we also need the `!important` here to be able to override the default hidden behavior on the sphinx rendered scikit-learn.org. See: https://github.com/scikit-learn/scikit-learn/issues/21755 */display: inline-block !important;position: relative;}#sk-container-id-1 div.sk-text-repr-fallback {display: none;}</style><div id=\"sk-container-id-1\" class=\"sk-top-container\"><div class=\"sk-text-repr-fallback\"><pre>KNeighborsClassifier()</pre><b>In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook. <br />On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.</b></div><div class=\"sk-container\" hidden><div class=\"sk-item\"><div class=\"sk-estimator sk-toggleable\"><input class=\"sk-toggleable__control sk-hidden--visually\" id=\"sk-estimator-id-1\" type=\"checkbox\" checked><label for=\"sk-estimator-id-1\" class=\"sk-toggleable__label sk-toggleable__label-arrow\">KNeighborsClassifier</label><div class=\"sk-toggleable__content\"><pre>KNeighborsClassifier()</pre></div></div></div></div></div>"
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 训练模型\n",
    "from sklearn.neighbors import KNeighborsClassifier\n",
    "estimator  = KNeighborsClassifier().fit(x_train,y_train)\n",
    "estimator"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "data": {
      "text/plain": "array([6644108708, 2337680907, 2571962720, ..., 1893125756, 6873618335,\n       6438240873], dtype=int64)"
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 预测\n",
    "y_pred = estimator.predict(x_test)\n",
    "y_pred"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "直接比对真实值和预测值:\n",
      " 3069885     False\n",
      "21047483    False\n",
      "16018865    False\n",
      "2981251     False\n",
      "14807850    False\n",
      "            ...  \n",
      "4647870      True\n",
      "7103167     False\n",
      "28482134    False\n",
      "1653994      True\n",
      "10831131     True\n",
      "Name: place_id, Length: 20800, dtype: bool\n"
     ]
    },
    {
     "data": {
      "text/plain": "0.35841346153846154"
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 模型评估\n",
    "print(\"直接比对真实值和预测值:\\n\", y_test == y_pred)\n",
    "\n",
    "# 准确率 35%\n",
    "estimator.score(x_test,y_test)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "0.35841346153846154"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.metrics import accuracy_score\n",
    "accuracy_score(y_test,y_pred)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}